package com.zhaiker.paddlesegmentationlib

import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.baidu.paddle.lite.demo.segmentation.PaddleSegHelper
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private lateinit var imageView : ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        imageView = findViewById(R.id.imageView)
        test()
    }

    private fun test(){

        lifecycleScope.launch {
            PaddleSegHelper.init(this@MainActivity)
            try {
                val imageStream = assets.open(getString(com.zhaiker.paddlesegmentation.R.string.IMAGE_PATH_DEFAULT))
                val image = BitmapFactory.decodeStream(imageStream)
                val bmp = PaddleSegHelper.segImage(image)
                imageView.setImageBitmap(bmp)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }


    }

}